using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using LitJson;
using mb;
using Xamarin.Essentials;

namespace libmp
{
    public static partial class Util
    {
        public static string BuildConfiguration =
#if DEBUG
            "DEBUG";
#else
        "RELEASE";
#endif

        static IPopManager _popMgr;
        public static IPopManager PopMgr
        {
            get
            {
                if (_popMgr == null)
                    _popMgr = ServiceLocator.Instance.Get<IPopManager>();

                return _popMgr;
            }
        }

        public static bool IsPhoneNo(string str_handset)
        {
            return Regex.IsMatch(str_handset, "^(0\\d{2,3}-?\\d{7,8}(-\\d{3,5}){0,1})|(((13[0-9])|(15([0-3]|[5-9]))|(18[0-9])|(17[0-9])|(14[0-9]))\\d{8})$");
        }

        public static bool IsEmail(string str_email)
        {
            return Regex.IsMatch(str_email, "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
        }

        public static string MD5Encrypt32(string password)
        {
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("x2");
            }
            return pwd;
        }

        public static int CurrentThreadId
        {
            get
            {
                return System.Threading.Thread.CurrentThread.ManagedThreadId;
            }
        }

        public static string GetHardwareInfo()
        {
            var jdev = new LitJson.JsonData();
            jdev["Manufacturer"] = DeviceInfo.Manufacturer;
            jdev["Model"] = DeviceInfo.Model;
            jdev["Name"] = DeviceInfo.Name;
            jdev["Platform"] = DeviceInfo.Platform.ToString();
            jdev["Idiom"] = DeviceInfo.Idiom.ToString();
            jdev["Type"] = DeviceInfo.DeviceType.ToString();
            jdev["ScreenWidth"] = DeviceDisplay.MainDisplayInfo.Width;
            jdev["ScreenHeight"] = DeviceDisplay.MainDisplayInfo.Height;

#if __IOS__
#else
            jdev["Id"] = mb.Droid.Util.GetSecureId();
#endif

            return JsonMapper.ToJson(jdev);
        }

        public static bool ContainsKey(this JsonData data, string key)
        {
            bool result = false;
            if (data == null)
                return result;
            if (!data.IsObject)
            {
                return result;
            }
            IDictionary tdictionary = data as IDictionary;
            if (tdictionary == null)
                return result;
            if (tdictionary.Contains(key))
            {
                result = true;
            }
            return result;
        }

        public static string FormatTime(int timeMs)
        {
            int totalSeconds = timeMs / 1000;

            int seconds = totalSeconds % 60;
            int minutes = (totalSeconds / 60) % 60;
            int hours = totalSeconds / 3600;

            if (hours > 0)
            {
                return string.Format("{0:0}:{1:00}:{2:00}", hours, minutes, seconds);
            }
            else
            {
                return string.Format("{0:00}:{1:00}", minutes, seconds);
            }
        }

    }
    public static partial class MyExtension
    {
        public static T TryGet<T>(this Dictionary<string, object> dict, string key) where T : class
        {
            object v;
            if (dict.TryGetValue(key, out v))
            {
                return v as T;
            }
            else
            {
                return null;
            }
        }

    }
}